import Vue from 'vue';
import Router from 'vue-router';

//重写路由
const routerPush = Router.prototype.push;
Router.prototype.goTo = function push(data) {
    this.history.state = -1; //去
    this.app.$store.dispatch('set_androidback', 1);
    return routerPush.call(this, data).catch(error => error)
};
Router.prototype.goTop = function push(data) {
    this.history.state = 0; //去动画 回顶级栈
    this.app.$store.dispatch('set_androidback', 0);
    return routerPush.call(this, data).catch(error => error)
};
const routerGo = Router.prototype.go;
Router.prototype.close = function push(data) {
    data = data ? data : -1;
    this.history.state = 1; //回
    if (!this.history || this.history.index == 1) {
        this.app.$store.dispatch('set_androidback', 0);
    } else {
        this.app.$store.dispatch('set_androidback', 1);
    }
    return routerGo.call(this, data);
}
Vue.use(Router);

//index=0 表示顶级栈可返回栈 注释：去动画
//index=1 表示顶级栈
//index>=2 子集栈

const routes = [
	{
        path: '*',
        redirect: '/',
        meta: {
            keepAlive: true,
            index: 1,
            title: '首页'
        }
    },
    {
        name: '/',
        component: () =>
            import ('./view/main/index.vue'),
        meta: {
            keepAlive: true,
            index: 1,
            title: '首页'
        }
    },
    
];

// add route path
routes.forEach(route => {
    route.path = route.path || '/' + (route.name || '');
});

const router = new Router({ routes });

router.beforeEach((to, from, next) => {
    const title = to.meta && to.meta.title;
    if (title) {
        document.title = title;
    }
    next();
});

export {
    router
};